package com.walle;

import java.io.File;
import java.io.FilenameFilter;

public class Main
{
	Object applicationContext;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) throws Exception
	{
		String configRoot = "config";

		if ((args != null) && (args.length > 0))
		{
			for (int i = 0; i < args.length; i++)
			{
				String cmd = args[i].toLowerCase();
				if ((cmd.equals("-n")) && (i < args.length - 1))
					System.setProperty("system.config.nodetype", args[(++i)]);
				else if ((cmd.equals("-conf")) && (i < args.length - 1))
				{
					configRoot = args[(++i)];
				}
			}
		}
		final ClassPath classpath = new ClassPath();
		File commonLibDir = new File("lib");
		commonLibDir.list(new FilenameFilter()
		{
			public boolean accept(File dir, String name)
			{
				if (name.endsWith(".jar"))
					classpath.addClasspath(dir.getPath() + "/" + name);
				return false;
			}
		});
		Thread.currentThread()
				.setContextClassLoader(classpath.getClassLoader());
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		Class clz = classLoader.loadClass("org.apache.log4j.PropertyConfigurator");
		clz.getMethod("configure", new Class[] { String.class }).invoke(null, new Object[] { "config/log4j.properties" });

		Main instance = new Main();

		clz = classLoader.loadClass("org.springframework.context.support.FileSystemXmlApplicationContext");
		instance.applicationContext = clz.getConstructor(new Class[] { String.class }).newInstance( new Object[] {  configRoot + "/spring/*-context.xml" });
	}
}
